﻿namespace Pps.Gui.Controls
{
    using Microsoft.SPOT;
    using Pps.Gui.Base;

    public enum TextAlignment : uint
    {
        Left = Bitmap.DT_AlignmentLeft,
        Right = Bitmap.DT_AlignmentRight,
        Center = Bitmap.DT_AlignmentCenter
    }

    public class Label : Control
    {
        private string text;
        private TextAlignment alignment;
        private bool autoSize;
        private bool wordWrap;

        private int width;
        private int height;

        public Label()
            : this(string.Empty, 0, 0)
        {
        }

        public Label(string text, int x, int y)
        {
            this.autoSize = true;
            this.wordWrap = true;

            this.Text = text;

            this.Position = new Point(x, y);
        }

        public bool AutoSize
        {
            get { return this.autoSize; }
            set
            {
                if (this.autoSize != value)
                {
                    this.autoSize = value;
                    this.CalculateSize();
                }
            }
        }

        public string Text
        {
            get { return this.text; }
            set
            {
                if (this.text != value)
                {
                    this.text = value;

                    this.CalculateSize();

                    this.Invalidate();
                }
            }
        }

        public TextAlignment TextAlignment
        {
            get { return this.alignment; }
            set
            {
                if (this.alignment != value)
                {
                    this.alignment = value;
                    this.Invalidate();
                }
            }
        }

        public bool WordWrap
        {
            get { return this.wordWrap; }
            set
            {
                if (this.wordWrap != value)
                {
                    this.wordWrap = value;
                    this.Invalidate();
                }
            }
        }

        public override Size Size
        {
            get { return base.Size; }
            set
            {
                if (base.Size != value)
                {
                    base.Size = value;

                    if (this.AutoSize == false)
                    {
                        this.Invalidate();
                    }
                }
            }
        }

        protected override void Paint(Graphics graphics)
        {
            graphics.DrawRectangle(this.BackgroundColor, 0, this.X, this.Y, this.Width, this.Height, 0, 0, this.BackgroundColor, 0, 0, this.BackgroundColor, 0, 0, 256);
            graphics.DrawTextInRect(this.text, this.X, this.Y, this.Width, this.Height, (uint)this.TextAlignment + (this.wordWrap == true ? Bitmap.DT_WordWrap : 0), this.ForegroundColor, this.Font);
        }

        private void CalculateSize()
        {
            if (this.autoSize)
            {
                this.Font.ComputeExtent(this.text, out this.width, out this.height);
                this.Size = new Size(this.width, this.height);
            }
        }
    }
}